import {ref} from 'vue'
import { defineStore } from 'pinia'

export const useCounterStore = defineStore('cart', () => {
  
  let carts=ref([])
  function addcart(detdata,goodsid){
    console.log('第一个参数',detdata);
    console.log('第二个参数',goodsid);
    
    let username=localStorage.getItem('username')
    let usecart=carts.value.filter(item=>item.username==username)
    let pdcart=usecart.find(item=>item.goods.goods_id==goodsid)
    if(pdcart){
      pdcart.number++
    }
    else{
      carts.value.push({
        username:username,
        id:Math.floor(Math.random()*1000000),
        goods:detdata,
        checked:false,
        number:1
      })
    }
  }
    return { carts,addcart}
  },
    {
    persist: true,
  }
)